package com.itheima.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.CategoryMapper;
import com.itheima.mapper.DishFlavorsMapper;
import com.itheima.mapper.DishMapper;
import com.itheima.pojo.*;
import com.itheima.service.DishService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@Log4j2
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorsMapper dishFlavorsMapper;
    @Autowired
    private CategoryMapper categoryMapper;


    /**
     * 添加菜品
     */
    @Transactional
    @Override
    public Result add(@RequestBody Dish dish) {
        // log.info(dish);
        QueryWrapper<Dish> wrapper = new QueryWrapper<>();
        wrapper.eq("name", dish.getName());
        Dish one = dishMapper.selectOne(wrapper);
        if (one != null) {
            return new Result(Code.OK, "菜品已存在", "{}");
        }

// TODO: 2022/10/17 类型

        Category category = categoryMapper.selectById(dish.getCategoryId());
        dish.setCategoryName(category.getName());
        dishMapper.insert(dish);


        for (DishFlavors f : dish.getFlavors()) {
            DishFlavors df = new DishFlavors();
            df.setDishId(dish.getId());
            df.setName(f.getName());
            df.setValue(f.getValue());
            dishFlavorsMapper.insert(df);
        }

        return new Result(Code.OK, "添加菜品成功", "{}");
    }


    /**
     * 菜品分页查询
     *
     * @param name
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Result page(String name, Integer page, Integer pageSize) {
        QueryWrapper wrapper = new QueryWrapper();
        if (!StrUtil.isBlank(name)) {
            wrapper.eq("name", name);
        }
        Page page1 = dishMapper.selectPage(new Page<>(page, pageSize), wrapper);
        return new Result(Code.OK, null, page1, "{}");
    }

    /**
     * 根据id查询菜品
     */
    @Override
    @Transactional
    public Result getid(String id) {
        Dish dish = dishMapper.selectById(id);
        // 查询口味表
        QueryWrapper<DishFlavors> wrapper = new QueryWrapper<>();
        wrapper.eq("dish_id", id);
        List<DishFlavors> list = dishFlavorsMapper.selectList(wrapper);
        // 重新装到菜品类里面
        dish.setFlavors(list);
        return new Result(Code.OK, null, dish, "{}");
    }


    /**
     * 删除菜品
     */
    @Override
    @Transactional
    public Result delect(String ids) {
//    1、创建集合删除两个表需要用的
        List<String> list = new ArrayList<>();
//        2、判断是否批量删除
        if (ids.contains(",")) {
            // 2.1、批量删除
            String[] split = ids.split(",");
            list = Arrays.asList(split);
            dishMapper.deleteBatchIds(list);
            // 3、删除flavors表中数据
            for (String s : list) {
                //         3.1、需要在里面创建对象
                QueryWrapper wrapper = new QueryWrapper<>();
                wrapper.eq("dish_id", s);
                dishFlavorsMapper.delete(wrapper);
            }
        } else {
            //        2.2、单个删除
            dishMapper.deleteById(ids);
            //         3.1、需要在里面创建对象
            QueryWrapper wrapper = new QueryWrapper<>();
            wrapper.eq("dish_id", ids);
            dishFlavorsMapper.delete(wrapper);
        }
        return new Result(Code.OK, null, "删除成功", "null");

    }


    /**
     * 起售停售
     *
     * @param status
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public Result update(int status, String ids) {
        // 创建一个集合
        List<String> list;
        // 判断是否需要批量修改
        if (ids.contains(",")) {
            //    批量删除取出分隔符
            String[] split = ids.split(",");
            //    放到集合
            list = Arrays.asList(split);
            //    遍历 把list集合李的id 一个个修改
            for (String s : list) {
                Dish dish = new Dish();
                dish.setId(s);
                dish.setStatus(status);
                dishMapper.updateById(dish);
            }
        } else {
            // 单个修改
            Dish dish = new Dish();
            dish.setId(ids);
            dish.setStatus(status);
            dishMapper.updateById(dish);

        }
        return new Result(Code.OK, null, null, "{}");
    }


    /**
     * 修改
     */
    @Override
    @Transactional
    public Result updatee(@RequestBody Dish dish) {
        //    更新菜品信息
        Category category = categoryMapper.selectById(dish.getCategoryId());
        dish.setCategoryName(category.getName());
        dishMapper.updateById(dish);


        //    更新口味信息
        //    根据id删除对应口味信息
        QueryWrapper wrapper1 = new QueryWrapper<>();
        wrapper1.eq("dish_id", dish.getId());
        dishFlavorsMapper.delete(wrapper1);
        //    重新添加口味信息
        List<DishFlavors> flavors = dish.getFlavors();
        DishFlavors dishFlavors = new DishFlavors();
        for (DishFlavors flavor : flavors) {
            flavor.setDishId(dish.getId());
        }
        dishFlavorsMapper.insert(dishFlavors);
        return new Result(Code.OK, "修改成功", "{}");
    }

    /**
     * 菜品下拉列表
     *
     * @param categoryId
     * @return
     */
    @Override
    @Transactional
    public Result getByCategoryId(String categoryId) {
        //获取dish菜品集合
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("category_id", categoryId);
        List<Dish> dishList = dishMapper.selectList(wrapper);
        //遍历dish菜品集合
        for (Dish dish : dishList) {
            QueryWrapper<DishFlavors> wrapper1 = new QueryWrapper();
            //    设置条件 eq比较
            wrapper1.eq("dish_id", dish.getId());
            //查询该菜品有几种口味
            List<DishFlavors> dishFlavorsList = dishFlavorsMapper.selectList(wrapper1);
            //把菜品口味放到集合里
            dish.setFlavors(dishFlavorsList);
        }
        return new Result(Code.OK, dishList, "{}");
    }


    /**
     * 移动端菜品列表
     *
     * @param
     * @param status
     * @return
     */
    @Override
    public Result checkAll(String categoryId, String status) {
        QueryWrapper<Dish> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id", categoryId);
        wrapper.eq("status", status);
        //查询菜品表
        List<Dish> dishlist = dishMapper.selectList(wrapper);
        //查询口味表
        for (Dish d : dishlist) {
            QueryWrapper<DishFlavors> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("dish_id", d.getId());
            List<DishFlavors> DFlist = dishFlavorsMapper.selectList(wrapper1);
            //重新装到菜品类
            d.setFlavors(DFlist);

        }
        return new Result(Code.OK, dishlist, "{}");
    }

}

